using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using Catastrophe.Log.UI;

namespace Catastrophe.Log
{
	public class ExceptionCache
	{
		private static readonly object LockObj = new object();

		private SystemExceptionFrm _exceptionFrm;

		public Action ClearHandler;

		public Queue<SysLogEntity> SystemErrorCache;

		public bool IsErrorShowDialog { get; set; } = true;


		public bool IsAllowSetNeverShow { get; set; } = true;


		public int MaxCache { get; set; } = 1000;


		public bool IsErrorRecords { get; set; }

		public bool IsFatal { get; set; }

		public string GetNewErrorFull
		{
			get
			{
				this.ShowErrorDialog();
				lock (ExceptionCache.LockObj)
				{
					if (this.SystemErrorCache.Count > 0)
					{
						return this.GetErrorInfoFull(this.SystemErrorCache.Last());
					}
					return "";
				}
			}
		}

		public string GetNewErrorSimple
		{
			get
			{
				this.ShowErrorDialog();
				string result = "";
				lock (ExceptionCache.LockObj)
				{
					if (this.SystemErrorCache.Count > 0)
					{
						result = this.GetErrorInfoSimple(this.SystemErrorCache.Last());
					}
					return result;
				}
			}
		}

		public ExceptionCache()
		{
			this.SystemErrorCache = new Queue<SysLogEntity>();
		}

		private void FatalOccurred(bool result)
		{
			this.IsFatal = result;
		}

		public Tuple<string, string> GetNewErrorSimpleCycle(string msgNow, string msgId)
		{
			this.GetNewErrorSimpleCycle(ref msgNow, ref msgId);
			return new Tuple<string, string>(msgNow, msgId);
		}

		public void GetNewErrorSimpleCycle(ref string msgNow, ref string msgId)
		{
			bool flag = false;
			try
			{
				this.ShowErrorDialog();
				lock (ExceptionCache.LockObj)
				{
					if (!string.IsNullOrWhiteSpace(msgId))
					{
						goto IL_0088;
					}
					SysLogEntity sysLogEntitycs = this.SystemErrorCache.FirstOrDefault((SysLogEntity x) => !string.IsNullOrWhiteSpace(x.DefineMsg));
					if (sysLogEntitycs != null)
					{
						msgNow = this.GetErrorInfoSimple(sysLogEntitycs);
						return;
					}
					msgNow = "";
					msgId = "";
					goto IL_0088;
					IL_0088:
					for (int i = 0; i < this.SystemErrorCache.Count; i++)
					{
						SysLogEntity sysLogEntitycs2 = this.SystemErrorCache.ElementAt(i);
						if (!flag)
						{
							flag = true;
							if (i != this.SystemErrorCache.Count - 1)
							{
								continue;
							}
							SysLogEntity sysLogEntitycs3 = this.SystemErrorCache.ElementAt(0);
							if (sysLogEntitycs3 != null)
							{
								msgNow = this.GetErrorInfoSimple(sysLogEntitycs3);
								break;
							}
						}
						if (flag && sysLogEntitycs2 != null)
						{
							msgNow = this.GetErrorInfoSimple(sysLogEntitycs2);
							break;
						}
					}
				}
			}
			catch (Exception innerException)
			{
				throw new Exception("循环显示Error", innerException);
			}
		}

		public Tuple<string, string> GetNewErrorFullCycle(string msgNow, string msgId)
		{
			this.GetNewErrorFullCycle(ref msgNow, ref msgId);
			return new Tuple<string, string>(msgNow, msgId);
		}

		public void GetNewErrorFullCycle(ref string msgNow, ref string msgId)
		{
			bool flag = false;
			try
			{
				this.ShowErrorDialog();
				lock (ExceptionCache.LockObj)
				{
					if (!string.IsNullOrWhiteSpace(msgId))
					{
						goto IL_0088;
					}
					SysLogEntity sysLogEntitycs = this.SystemErrorCache.FirstOrDefault((SysLogEntity x) => !string.IsNullOrWhiteSpace(x.DefineMsg));
					if (sysLogEntitycs != null)
					{
						msgNow = this.GetErrorInfoFull(sysLogEntitycs);
						return;
					}
					msgNow = "";
					msgId = "";
					goto IL_0088;
					IL_0088:
					for (int i = 0; i < this.SystemErrorCache.Count; i++)
					{
						SysLogEntity sysLogEntitycs2 = this.SystemErrorCache.ElementAt(i);
						if (!flag)
						{
							flag = true;
							if (i != this.SystemErrorCache.Count - 1)
							{
								continue;
							}
							SysLogEntity sysLogEntitycs3 = this.SystemErrorCache.ElementAt(0);
							if (sysLogEntitycs3 != null)
							{
								msgNow = this.GetErrorInfoFull(sysLogEntitycs3);
								break;
							}
						}
						if (flag && sysLogEntitycs2 != null)
						{
							msgNow = this.GetErrorInfoFull(sysLogEntitycs2);
							break;
						}
					}
				}
			}
			catch (Exception innerException)
			{
				throw new Exception("循环显示Error", innerException);
			}
		}

		public void Add(SysLogEntity error)
		{
			try
			{
				lock (ExceptionCache.LockObj)
				{
					SysLogEntity sysLogEntitycs = this.SystemErrorCache.FirstOrDefault((SysLogEntity x) => x.DefineMsg.Equals(error.DefineMsg));
					if (sysLogEntitycs != null)
					{
						sysLogEntitycs.TimeStamp = error.TimeStamp;
						sysLogEntitycs.ErrorCode = error.ErrorCode;
						return;
					}
					Thread.Sleep(1);
					if (this.IsErrorRecords)
					{
						LogSession.DbLog.WriteLogNormal(LogLevelEnum.Fatal, this.GetErrorInfoFull(error), null, "", "Add", "D:\\Csharp\\PeanutNet6.0\\Catastrophe.Log\\Catastrophe.Log\\ExceptionCache.cs", 291);
					}
					this.SystemErrorCache.Enqueue(error);
					if (this.SystemErrorCache.Count > this.MaxCache)
					{
						this.SystemErrorCache.Dequeue();
					}
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public void RemoveAll()
		{
			lock (ExceptionCache.LockObj)
			{
				this.SystemErrorCache.Clear();
			}
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			lock (ExceptionCache.LockObj)
			{
				foreach (SysLogEntity item in this.SystemErrorCache)
				{
					stringBuilder.Append(this.GetErrorInfoFull(item));
				}
			}
			return stringBuilder.ToString();
		}

		public string GetErrorInfoFull(SysLogEntity error)
		{
			if (error == null)
			{
				return "";
			}
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = stringBuilder;
			StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(65, 11, stringBuilder2);
			handler.AppendLiteral("异常等级");
			handler.AppendFormatted(error.ErrorLevel);
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendLiteral("自定义消息");
			handler.AppendFormatted(error.DefineMsg);
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendLiteral("时间戳");
			handler.AppendFormatted(error.TimeStamp);
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendLiteral("错误编码");
			handler.AppendFormatted(error.ErrorCode);
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendLiteral("/////////////////////////////////////////////////");
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendFormatted(Environment.NewLine);
			handler.AppendFormatted(Environment.NewLine);
			stringBuilder2.Append(ref handler);
			return stringBuilder.ToString();
		}

		private void ShowErrorDialog()
		{
			try
			{
				if (!this.IsErrorShowDialog)
				{
					return;
				}
				if (this._exceptionFrm == null || !(this._exceptionFrm.Visibility==Visibility.Visible))
				{
					if (this.IsFatal)
					{
						if (this._exceptionFrm == null)
						{
							this._exceptionFrm = new SystemExceptionFrm(this)
							{
								ClearHandler = this.ClearHandler
							};
						}
						this._exceptionFrm.ShowDialog();
					}
				}
				else if (!this.IsFatal)
				{
					this._exceptionFrm?.Close();
					this._exceptionFrm = null;
				}
				else if (this._exceptionFrm.IsInitialized)
				{
					this._exceptionFrm?.mode.Refresh.Execute(null);
				}
			}
			catch (Exception innerException)
			{
				throw new Exception("显示异常窗体失败！", innerException);
			}
		}


		public string GetErrorInfoSimple(SysLogEntity error)
		{
			if (error == null)
			{
				return "";
			}
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = stringBuilder;
			StringBuilder.AppendInterpolatedStringHandler handler = new StringBuilder.AppendInterpolatedStringHandler(1, 2, stringBuilder2);
			handler.AppendFormatted(error.DefineMsg);
			handler.AppendLiteral(" ");
			handler.AppendFormatted(error.TimeStamp);
			stringBuilder2.Append(ref handler);
			return stringBuilder.ToString();
		}
	}
}
